//
//  ViewController.m
//  17-HomeWork-通讯录
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "ContactsTableViewCell.h"
#import "Contacts.h"
#import "InformationViewController.h"
#import "AddContactViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, SaveContactDelegate>
@property (weak, nonatomic) IBOutlet UITableView *contactTableView;
@property (nonatomic, strong) NSMutableArray *arrayOfData;
@property (nonatomic, strong) NSString *contactPath;
@property (nonatomic, strong) NSMutableArray *arrayOfTitle;
@property (nonatomic, strong) NSMutableSet *set;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
    [self getPath];
    [self unarcheiveContact];
    [self compareArray];
    
}
- (void)compareArray{
    
}
// 自定义保存按钮的协议方法
- (void)saveName:(NSString *)name phoneNumber:(NSString *)phoneNumber address:(NSString *)address {
    if (name.length > 0) {
        Contacts *addContact = [[Contacts alloc] initWithHeadImageString:@"0" nameString:name phoneNumber:phoneNumber addressString:address];
        [_arrayOfData addObject:addContact];
        [self contactArchive];
        [_contactTableView reloadData];
        
    }
}
#pragma mark - tableView Datasource
//- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//    _arrayOfTitle = [NSMutableArray array];
//    _set = [NSMutableSet set];
//    for (Contacts *contact in _arrayOfData) {
//        if (contact.nameString.length > 0) {
//            [_set addObject:[NSString stringWithFormat:@"%c",[contact.nameString characterAtIndex:0]].capitalizedString];
//        }
//    }
//    _arrayOfTitle = [_set allObjects].mutableCopy;
//    [_arrayOfTitle sortUsingSelector:@selector(compare:)];
//  
//    return _arrayOfTitle;
//}
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return _arrayOfTitle.count;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return _arrayOfData.count;
}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    return [@(section) description];
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ContactsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    Contacts *contact = _arrayOfData[indexPath.row];
    cell.labelOfName.text = contact.nameString;
    return cell;
}
#pragma mark - 两个界面之间的桥梁
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([sender isKindOfClass:[ContactsTableViewCell class]]) {
        ContactsTableViewCell *cell = sender;
        NSIndexPath *indexPath = [_contactTableView indexPathForCell:cell];
        InformationViewController *information = [[InformationViewController alloc] init];
        Contacts *contact = _arrayOfData[indexPath.row];
        information = segue.destinationViewController;
        information.contact = contact;
    } else {
        AddContactViewController *addContact = [[AddContactViewController alloc] init];
        addContact = segue.destinationViewController;
        addContact.delegate = self;
    }
}
// 进入编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    
    [_contactTableView setEditing:editing animated:animated];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_arrayOfData removeObjectAtIndex:indexPath.row];
        [self contactArchive];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
#pragma mark - 存储信息到本地
// 获取文件路径
- (void)getPath {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSLog(@"%@", [pathArray firstObject]);
    self.contactPath = [[pathArray firstObject]stringByAppendingPathComponent:@"contact.plist"];
}
// 反归档
- (void)unarcheiveContact {
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:_contactPath];
    _arrayOfData = [NSArray arrayWithArray:array].mutableCopy;
}
// 归档
- (void)contactArchive {
    [NSKeyedArchiver archiveRootObject:_arrayOfData toFile:_contactPath];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
